//
//  TransactionViewController.m
//  NewPro
//
//  Created by RYX on 2018/11/23.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "TransactionViewController.h"
#import "TransactionCell.h"
#import "TransactionDetailViewController.h"
#import "TransactionStore.h"
#import "MerchantModel.h"
#import "MerchantsDetailModel.h"
#import "MerchantList.h"

@interface TransactionViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,copy) NSArray *listArr;

@end

@implementation TransactionViewController

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 0.5;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 5)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        
        MJWeakSelf
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [weakSelf refresh];
        }];
        
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
    }
    
    return _tableView;
}

- (void)refresh{
    
    TransactionStore *store = [[TransactionStore alloc] init];
    
    NSString *customId = [UserDefaultsManager sharedInstance].user_id;
    
    MJWeakSelf
    [store getMerchantListWithCustomId:customId Success:^(NSArray *listArr) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.listArr = listArr;
        [weakSelf.tableView reloadData];
        
    } Failure:^(NSError *error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
    
}

- (void)setUI{
    
    [self setNavBarWithTitle:@"交易明细"];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.bottom.equalTo(self.view);
        
    }];
    
    [self.tableView.mj_header beginRefreshing];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [TransactionCell getHeight];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    MerchantModel *model = self.listArr[section];
    
    return model.merchants.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MerchantModel *model = self.listArr[indexPath.section];
    NSArray *arr = model.merchants;
    NSArray *mArr = [MerchantsDetailModel mj_objectArrayWithKeyValuesArray:arr];
    MerchantsDetailModel *cellModel = mArr[indexPath.row];
    
    TransactionCell *cell = [TransactionCell tempWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.model = cellModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MerchantModel *model = self.listArr[indexPath.section];
    NSArray *arr = model.merchants;
    NSArray *mArr = [MerchantsDetailModel mj_objectArrayWithKeyValuesArray:arr];
    MerchantsDetailModel *cellModel = mArr[indexPath.row];
    cellModel.month = model.month;
    
    [MerchantList sharedInstance].selectedModel = cellModel;
    
    TransactionDetailViewController *VC = [[TransactionDetailViewController alloc] init];
    VC.month = model.month;
    [self.navigationController pushViewController:VC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 25;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    MerchantModel *model = self.listArr[section];
    
    UIFont *font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
    UIColor *color = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 25)];
    UILabel *la = [[UILabel alloc] init];
    la.font = font;
    la.textColor = color;
    la.textAlignment = NSTextAlignmentLeft;
    la.text = model.month;
    [view addSubview:la];
    [la mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(20);
        make.left.equalTo(view).offset(14);
    }];
    
    
    UILabel *la2 = [[UILabel alloc] init];
    la2.font = font;
    la2.textColor = color;
    la2.textAlignment = NSTextAlignmentRight;
    la2.text = @"总交易额（万元）";
    [view addSubview:la2];
    [la2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(20);
        make.right.equalTo(view).offset(-14);
    }];
    
    return view;
}

@end
